//
//  JLXXTestController.m
//  JLXXBase
//
//  Created by apple on 17/5/11.
//  Copyright © 2017年 cnsuer. All rights reserved.
//

#import "JLXXTestController.h"
#import "JLXXMineProductionTitleView.h"
#import "JLXXMineProductionScrollView.h"
#import "JLXXRecordingController.h"
#import "JLXXVideosController.h"
#import "JLXXPhotosController.h"
#import "UIView+JLXXFrame.h"
#import "JLXXDefine.h"

@interface JLXXTestController ()<UIScrollViewDelegate>

@property (nonatomic , strong) JLXXMineProductionScrollView *scrollView;

@property (nonatomic , strong) JLXXMineProductionTitleView *titleView;


@end

@implementation JLXXTestController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addMainScrollView];
    [self addChildController];
}

-(void)addChildController{
    
    JLXXPhotosController *photos = [[JLXXPhotosController alloc]init];
    [self addChildViewController:photos];
    JLXXVideosController *videos = [[JLXXVideosController alloc]init];
    [self addChildViewController:videos];
    JLXXRecordingController *recording = [[JLXXRecordingController alloc]init];
    [self addChildViewController:recording];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:_titleView];
    if (!_titleView) {
        [self.titleView selectButtonForTag:0];
    }
}


-(JLXXMineProductionTitleView *)titleView{
    if (!_titleView) {
        _titleView = [JLXXMineProductionTitleView titleViewFromXib];
        _titleView.frame = CGRectMake(0, 0, 200, 44);
        _titleView.yzgCenterX = self.navigationController.navigationBar.yzgCenterX;
        [self.navigationController.navigationBar addSubview:self.titleView];
        KWeakSelf;
        self.titleView.selectedButtonTag =  ^(NSInteger index) {
            [ws.scrollView scrollToTableViewWithTag:index];
        };
        self.titleView.search =^(){
            NSLog(@"searchButtonClick");
        };
        self.titleView.rank = ^(){
            NSLog(@"rankButtonClick");
        };
    }
    return _titleView;
}

-(void)addMainScrollView
{
    self.scrollView = ({
        JLXXMineProductionScrollView *scrollView = [[JLXXMineProductionScrollView alloc]init];
        scrollView.delegate = self;
        scrollView.frame = self.view.bounds;
        [self.view addSubview:scrollView];
        scrollView.contentSize = CGSizeMake(3 *KScreenWidth, 0);
        scrollView;
    });
    
}


#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    CGFloat width = scrollView.frame.size.width;
    CGFloat height = scrollView.frame.size.height;
    CGFloat offsetX = scrollView.contentOffset.x;
    // 当前位置需要显示的控制器的索引
    NSInteger index = offsetX / width;
    // 取出需要显示的控制器
    UIViewController *willShowVc = self.childViewControllers[index];
    //选中相应标题按钮
    [self.titleView selectButtonForTag:index];
    // 如果当前位置的位置已经显示过了，就直接返回
    if ([willShowVc isViewLoaded]) return;
    // 添加控制器的view到contentScrollView中;
    willShowVc.view.frame = CGRectMake(offsetX, 0, width, height);
    [scrollView addSubview:willShowVc.view];
}

/**
 * 手指松开scrollView后，scrollView停止减速完毕就会调用这个
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

@end
